// muyu.js
// 获取应用实例
const app = getApp();
const clickAudioContext = wx.createInnerAudioContext({
  useWebAudioImplement: true
});
const bgAudioContext = wx.createInnerAudioContext({
  useWebAudioImplement: false
});
var setInter;
Page({
  data: {
    classStyle: '',
    value: 0,
    id: 'default',
    musicimg: '/images/Stop.ico',
    music: false,
    restartimg: '/images/Reload.ico',
    restart: false,
    autoimg: '/images/Refresh.ico',
    auto: false,
    // bg:null,
    // userInfo: {},
    // hasUserInfo: false,
    // canIUse: wx.canIUse('button.open-type.getUserInfo'),
    // canIUseGetUserProfile: false,
    // canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad(e) {
    try {
      let gongde = 0;
      var value = wx.getStorageSync('gongde')
      console.log('value:' + JSON.stringify(value))
      if (undefined != value && '' != value) {
        gongde = parseInt(value);
      } else {
        console.log('options:' + JSON.stringify(e))
        if (undefined != e && Object.keys(e).length != 0) {
          gongde = parseInt(e.gongde);
          wx.showToast({
            title: '您的朋友与您共享了' + gongde + '功德，善哉',
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
      this.setData({
        value: gongde
      });
    } catch (e) {
      // Do something when catch error
    }
    
    bgAudioContext.src = 'http://music.163.com/song/media/outer/url?id=185569.mp3';
    bgAudioContext.seek(21);
    bgAudioContext.loop = true;
    //bgAudioContext.play(); // 播放
    clickAudioContext.src = '/audios/qiao.wav';
  },
  //监听页面卸载
  onUnload: function () {
    autoStop();
    musicStop();
  },
    /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    autoStop();
    musicStop();
  },
  onShareAppMessage() {
    return {
      title: '今日我已功德 +' + this.data.value + '，你也一起来敲电子木鱼吧！',
      path: '/pages/muyu/muyu?gongde=' + this.data.value
    }
  },
  click() {
    clickAudioContext.play(); //播放音乐
    this.setData({
      classStyle: 'animal',
      id: 'font',
      value: this.data.value + 1,
    });
    wx.setStorage({
      key:"gongde",
      data:this.data.value
    })
    setTimeout(() => {
      this.setData({
        classStyle: '',
      });
    }, 100); //图片还原
    setTimeout(() => {
      this.setData({
        id: 'default'
      });
    }, 500); //功德标签还原
  },
  restartFun: function () {
    wx.showModal({
      title: '提示',
      content: '是否重新积攒功德',
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          this.setData({
            value: 0,
            auto: false,
            autoimg: '/images/Pause.ico',
            musicimg: '/images/Stop.ico'
          });
          clearInterval(setInter);
          bgAudioContext.stop();
        }
      }
    })
  },
  autoFun: function () {
    if (this.data.auto) {
      autoStop();
    } else {
      this.setData({
        auto: true,
        autoimg: '/images/Pause.ico'
      });
      setInter = setInterval(() => {
        this.click();
      }, 1000);
    }
  },
  autoStop: function () {
    this.setData({
      auto: false,
      autoimg: '/images/Refresh.ico'
    });
    clearInterval(setInter);
  },
  musicFun: function () {
    if (this.data.music) {
      musicStop();
    } else {
      musicPlay();
    }
  },
  musicStop: function () {
    this.setData({
      music: false,
      musicimg: '/images/Stop.ico'
    });
    bgAudioContext.pause();
  },
  musicPlay: function () {
    this.setData({
      music: true,
      musicimg: '/images/Play.ico'
    });
    bgAudioContext.play();
  }
})